#define _CRT_SECURE_NO_WARNINGS
#include"game.h"
extern int count;

typedef struct
{
	int mine;
	int count;
	int reveal;
	char show;
	int flag;
} MineField;

MineField mine[HS][LS];

void Menu()
{
	printf("\033[36m");
	printf("***********************************\n");
	printf("********  Mine Sweeper V3  ********\n");
	printf("***********************************\n");
	printf("********\033[32m      1. Play\033[36m      ********\n");
	printf("********\033[31m      0. Exit\033[36m      ********\n");
	printf("***********************************\n");
	printf("***********************************\n");
	printf("\033[34mPlay?[\033[32m1\033[34m/\033[31m0\033[34m]\033[0m\n");
	printf("\033[0m");
}

void Initialize()
{
	int i = 0, j = 0;
	for (i = 0; i < HS; i++)
	{
		for (j = 0; j < LS; j++)
		{
			mine[i][j].mine = 0;
			mine[i][j].count = 0;
			mine[i][j].reveal = 0;
			mine[i][j].show = '*';
			mine[i][j].flag = 0;
		}
	}
}

void Place()
{
	int count2 = count;
	for (;count2 > 0;)
	{
		int a = (rand() % H) + 1;
		int b = (rand() % L) + 1;
		if (mine[a][b].mine == 0)
		{
			mine[a][b].mine = 1;
			count2--;
		}
	}
}

void Count()
{
	int i = 1, j = 1;
	for (i = 1; i <= H; i++)
	{
		for (j = 1; j <= L; j++)
		{
			if (0 == mine[i][j].mine)
			{
				mine[i][j].count =
				mine[i - 1][j - 1].mine +
				mine[i - 1][j].mine +
				mine[i - 1][j + 1].mine +
				mine[i][j - 1].mine +
				mine[i][j + 1].mine +
				mine[i + 1][j - 1].mine +
				mine[i + 1][j].mine +
				mine[i + 1][j + 1].mine;
			}
		}
	}
}

void Show(int z)
{
	int i = 1, j = 1;
	
	for (i = 1; i <= H; i++)
	{
		printf("---");
	}
	printf("\n");
	printf("\n\n  0\033[A");
	for (i = 1; i <= H; i++)
	{
		printf("\033[A%d\033[B\033[D%d ", i / 10, i % 10);
	}
	printf("\033[E\033[3C");
	for (i = 1; i <= H; i++)
	{
		printf("--");
	}
	printf("\n");
	for (i = 1; i <= H; i++)
	{
		printf("%.2d|", i);
		for (j = 1; j <= L; j++)
		{
			if (0 == z)
			{
				if (mine[i][j].show == '*')
				{
					printf("\033[36m%c\033[0m ", mine[i][j].show);
				}
				else if(mine[i][j].show == '!')
				{
					printf("\033[41m%c\033[0m ", mine[i][j].show);
				}
				else
				{
					printf("%c ", mine[i][j].show);
				}
			}
			else if (1 == z)
			{
				if (1 == mine[i][j].mine)
				{
					mine[i][j].show = '!';
				}
				else
				{
					mine[i][j].show = ' ';
				}
				if (mine[i][j].show == ' ')
				{
					printf("%c ", mine[i][j].show);
				}
				else
				{
					printf("\033[41m%c\033[0m ", mine[i][j].show);
				}
			}
		}
		printf("\n");
	}
	for (i = 1; i <= H; i++)
	{
		printf("---");
	}
	printf("\n");
}

int Reveal()
{

	while (1)
	{
		int choose = 0;
		printf("\033[35mReveal/(De)Flag[1/2]\033[0m\n");
		Clear();
		scanf("%d", &choose);
		switch (choose)
		{
		case 1:
		{
			int x, y;
			printf("\033[34mEnter the coordinates.\033[0m\n");
			Clear();
			scanf("%d %d", &x, &y);
			if (x < 1 || y < 1 || x > H || y > L)
			{
				printf("\033[31mTyping error.\033[0m\n");
				continue;
			}
			else if (1 == mine[x][y].flag)
			{
				printf("\033[33mThat slot is a flag.\033[0m\n");
				continue;
			}
			else if (1 == mine[x][y].reveal)
			{
				printf("\033[33mThat slot was revealed.\033[0m\n");
				continue;
			}
			else if (1 == mine[x][y].mine)
			{
				printf("\033[41m\033[1mYou have lost.\033[0m\n");
				Show(1);
				return 0;
			}
			else
			{
				Spread(x, y);
				return 1;
			}
			break;
		}
		case 2:
		{
			int x, y;
			printf("\033[34mEnter the coordinates.\033[0m\n");
			Clear();
			scanf("%d %d", &x, &y);
			if (x < 1 || y < 1 || x > H || y > L)
			{
				printf("\033[31mTyping error.\033[0m\n");
				continue;
			}
			else if (1 == mine[x][y].flag)
			{
				mine[x][y].flag = 0;
				mine[x][y].show = '*';
				return 1;
			}
			else if (1 == mine[x][y].reveal)
			{
				printf("\033[33mThat slot is revealed.\033[0m\n");
			}
			else
			{
				mine[x][y].show = '!';
				mine[x][y].flag = 1;
				return 1;
			}
			break;
		}
		default:
		{
			printf("\033[31mTyping error.\033[0m\n");
			continue;
		}
		}
	}
}

void Spread(int x, int y)
{
	if (x <= H && x >= 1 && y <= L && y >= 1 && 0 == mine[x][y].reveal && 0 == mine[x][y].mine)
	{
		//Show(0);
		if (0 == mine[x][y].count)
		{
			mine[x][y].show = ' ';
			mine[x][y].reveal = 1;
			mine[x][y].flag = 0;
		}
		else
		{
			mine[x][y].show = mine[x][y].count + 48;
			mine[x][y].reveal = 1;
			mine[x][y].flag = 0;
		}

		if (0 == mine[x][y].count)
		{
			Spread(x - 1, y - 1);
			Spread(x - 1, y);
			Spread(x - 1, y + 1);
			Spread(x, y - 1);
			Spread(x, y + 1);
			Spread(x + 1, y - 1);
			Spread(x + 1, y);
			Spread(x + 1, y + 1);
		}
	}

}

void Clear()
{
	int a;
	while ((a = getchar()) != '\n' && a != EOF);
}

int Judge()
{
	int reveal = H * L - count;
	int i = 1, j = 1;
	for (i = 1; i <= H; i++)
	{
		for (j = 1; j <= L; j++)
		{
			if (1 == mine[i][j].reveal)
			{
				reveal--;
			}
			if (0 == reveal)
			{
				return 1;
			}
		}

	}

}

int Game()
{
	Initialize();
	Place();
	Count();
	while (1)
	{
		Show(0);
		if (0 == Reveal())
		{
			return 0;
		}
		if(1 == Judge())
		{
			printf("\033[42m\033[1mYou win!\033[0m\n");
			Show(1);
			return 1;
		}
	}
}

